package com.cyanite.playlv2;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class StartPageActivity extends Activity{

    private int mStep = 0;
    private ViewWidget mWidgets;
    
    private BroadcastReceiver mLoggedOutReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            finish();
        }
    };


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.start_page_activity);
        registerReceiver(mLoggedOutReceiver, new IntentFilter(PlayLv2.INTENT_ACTION_LOGGED_OUT));

        ensureUi();
    }

    private void showPageOne(){
        mWidgets.mStepIcon.setBackgroundResource(R.drawable.start_step_one);
        mWidgets.mStepInfo.setText(R.string.start_info_one);
        mWidgets.mPhoto.setImageResource(R.drawable.start_page_one_picture);
        mWidgets.mLeftButton.setVisibility(View.GONE);
        mWidgets.mRightButton.setText(R.string.trip_next);
        mWidgets.InfoOne.setText(R.string.start_page_one_info_one);
        mWidgets.InfoTwo.setText(R.string.start_page_one_info_two);
        mWidgets.InfoThree.setText(R.string.start_page_one_info_three);
    }

    private void showPageTwo(){
        mWidgets.mStepIcon.setBackgroundResource(R.drawable.start_step_two);
        mWidgets.mStepInfo.setText(R.string.start_info_two);
        mWidgets.mPhoto.setImageResource(R.drawable.start_page_two_picture);
        mWidgets.mLeftButton.setVisibility(View.VISIBLE);
        mWidgets.mLeftButton.setText(R.string.trip_pre);
        mWidgets.InfoOne.setText(R.string.start_page_two_info_one);
        mWidgets.InfoTwo.setText(R.string.start_page_two_info_two);
        mWidgets.InfoThree.setText(null);
    }
    
    private void showPageThree(){
        mWidgets.mStepIcon.setBackgroundResource(R.drawable.start_step_three);
        mWidgets.mStepInfo.setText(R.string.start_info_three);
        mWidgets.mPhoto.setImageResource(R.drawable.start_page_three_picture);
        mWidgets.mLeftButton.setVisibility(View.VISIBLE);
        mWidgets.mLeftButton.setText(R.string.trip_pre);
        mWidgets.mRightButton.setText(R.string.trip_done);
        mWidgets.InfoOne.setText(R.string.start_page_three_info_one);
        mWidgets.InfoTwo.setText(R.string.start_page_three_info_two);
        mWidgets.InfoThree.setText(R.string.start_page_three_info_three);
    }

    private void showPage(int index){
        switch(index){
            case 0:
                showPageOne();
                break;
            case 1:
                showPageTwo();
                break;
            case 2:
                showPageThree();
                break;
        }
    }

    private void ensureUi(){

        mWidgets = new ViewWidget();
        mWidgets.mStepIcon = (ImageView)findViewById(R.id.ivStepIcon);
        mWidgets.mStepInfo = (TextView)findViewById(R.id.tvStepInfo);
        mWidgets.mPhoto = (ImageView)findViewById(R.id.ivStepPhoto);
        mWidgets.InfoOne = (TextView)findViewById(R.id.tvStartInfoOne);
        mWidgets.InfoTwo = (TextView)findViewById(R.id.tvStartInfoTwo);
        mWidgets.InfoThree = (TextView)findViewById(R.id.tvStartInfoThree);
        mWidgets.mLeftButton = (Button)findViewById(R.id.ibLeft);
        mWidgets.mRightButton = (Button)findViewById(R.id.ibRight);

        mWidgets.mLeftButton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
               if (mStep > 0)   {
                   showPage(--mStep);
                }
            }
        });
        mWidgets.mRightButton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                if ( mStep < 2 )    {
                    showPage(++mStep);
                } else {
                    Intent intent = new Intent(StartPageActivity.this, MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                    finish();
                }                
            }
        });
        showPageOne();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

     @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mLoggedOutReceiver);
    }

    private static class  ViewWidget{
        ImageView mStepIcon;
        TextView mStepInfo;
        ImageView mPhoto;
        TextView InfoOne;
        TextView InfoTwo;
        TextView InfoThree;
        Button mLeftButton;
        Button mRightButton;
    }
}
